package com.yuezhu.com_volley;

import com.android.volley.Cache;
import com.android.volley.Network;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.ResponseDelivery;

/**
 * 将tag打入到request queue中
 */
public class OkRequestQueue extends RequestQueue {
    private String mTag;
    /**
     * 构造器，（参考Volley源码）
     * @param cache
     * @param network
     * @param threadPoolSize
     * @param delivery
     */
    public OkRequestQueue(Cache cache, Network network, int threadPoolSize,
                          ResponseDelivery delivery) {
        super(cache, network, threadPoolSize, delivery);

        genTagIfNull();
    }

    public OkRequestQueue(Cache cache, Network network, int threadPoolSize) {
        super(cache, network, threadPoolSize);

        genTagIfNull();
    }

    public OkRequestQueue(Cache cache, Network network) {
        super(cache, network);

        genTagIfNull();
    }

    private void genTagIfNull(){

        if(mTag == null) {
            mTag = OkNetUtil.generateTag();
        }
    }

    @Override
    public Request add(Request request) {

        request.setTag(mTag);

        return super.add(request);
    }

    /**
     * 取消所有当前队列中的请求
     */
    public void cancelAll() {

        if(mTag != null) {
            super.cancelAll(mTag);
        }
    }

}
